﻿using FlyingEye.Devices;
using FlyingEye.Devices.Repositories;
using FlyingEye.Exceptions;
using System.Net;
using Volo.Abp.Application.Services;
using Volo.Abp.Validation;

namespace FlyingEye.DeviceServices
{
    public class DeviceConfigurationService : ApplicationService
    {
        private readonly IDeviceConfigurationRepository _deviceConfigurationRepository;

        public DeviceConfigurationService(IDeviceConfigurationRepository deviceConfigurationRepository)
        {
            _deviceConfigurationRepository = deviceConfigurationRepository;
        }

        /// <summary>
        /// 删除设备配置
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            await _deviceConfigurationRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 获取设备配置
        /// </summary>
        public async Task<List<DeviceConfigurationResponse>> GetListAsync()
        {
            var list = await _deviceConfigurationRepository.GetListAsync();

            return ObjectMapper.Map<List<DeviceConfigurationModel>, List<DeviceConfigurationResponse>>(list);
        }

        /// <summary>
        /// 获取设备配置
        /// </summary>
        public async Task<DeviceConfigurationResponse> GetAsync(Guid id)
        {
            var model = await _deviceConfigurationRepository.GetAsync(id);

            return ObjectMapper.Map<DeviceConfigurationModel, DeviceConfigurationResponse>(model);
        }

        /// <summary>
        /// 更新设备配置
        /// </summary>
        public async Task<DeviceConfigurationResponse> UpdateAsync(UpdateDeviceConfigurationRequest deviceConfiguration)
        {
            deviceConfiguration.ResourceId = deviceConfiguration.ResourceId.Trim();

            if (string.IsNullOrWhiteSpace(deviceConfiguration.ResourceId))
            {
                throw new AppValidationException("设备资源号不能为空！");
            }

            deviceConfiguration.DeviceName = deviceConfiguration.DeviceName.Trim();

            if (string.IsNullOrWhiteSpace(deviceConfiguration.DeviceName))
            {
                throw new AppValidationException("设备逻辑名不能为空！");
            }

            deviceConfiguration.IPAddress = deviceConfiguration.IPAddress.Trim();

            if (string.IsNullOrWhiteSpace(deviceConfiguration.IPAddress))
            {
                throw new AppValidationException("IP 地址不能为空！");
            }

            if (!IPAddress.TryParse(deviceConfiguration.IPAddress, out var _))
            {
                throw new AppValidationException("IP 地址格式不正确！");
            }

            if (deviceConfiguration.Port <= 0 || deviceConfiguration.Port > 65535)
            {
                throw new AppValidationException("设备端口超限！");
            }

            deviceConfiguration.DeviceType = deviceConfiguration.DeviceType.Trim();

            if (!PlcTypes.PlcTypeList.Contains(deviceConfiguration.DeviceType))
            {
                throw new AppValidationException("设备类型设置错误！");
            }

            var model = await _deviceConfigurationRepository.GetAsync(deviceConfiguration.Id);

            ObjectMapper.Map(deviceConfiguration, model);

            model = await _deviceConfigurationRepository.UpdateAsync(model);

            return ObjectMapper.Map<DeviceConfigurationModel, DeviceConfigurationResponse>(model);
        }

        /// <summary>
        /// 插入客户端
        /// </summary>
        public async Task<DeviceConfigurationResponse> InsertAsync(CreateDeviceConfigurationRequest deviceConfiguration)
        {
            deviceConfiguration.ResourceId = deviceConfiguration.ResourceId.Trim();

            if (string.IsNullOrWhiteSpace(deviceConfiguration.ResourceId))
            {
                throw new AppValidationException("设备资源号不能为空！");
            }

            deviceConfiguration.DeviceName = deviceConfiguration.DeviceName.Trim();

            if (string.IsNullOrWhiteSpace(deviceConfiguration.DeviceName))
            {
                throw new AppValidationException("设备逻辑名不能为空！");
            }

            deviceConfiguration.IPAddress = deviceConfiguration.IPAddress.Trim();

            if (string.IsNullOrWhiteSpace(deviceConfiguration.IPAddress))
            {
                throw new AppValidationException("IP 地址不能为空！");
            }

            if (!IPAddress.TryParse(deviceConfiguration.IPAddress, out var _))
            {
                throw new AppValidationException("IP 地址格式不正确！");
            }

            if (deviceConfiguration.Port <= 0 || deviceConfiguration.Port > 65535)
            {
                throw new AppValidationException("设备端口超限！");
            }

            deviceConfiguration.DeviceType = deviceConfiguration.DeviceType.Trim();

            if (!PlcTypes.PlcTypeList.Contains(deviceConfiguration.DeviceType))
            {
                throw new AppValidationException("设备类型设置错误！");
            }

            var model = ObjectMapper.Map<CreateDeviceConfigurationRequest, DeviceConfigurationModel>(deviceConfiguration);

            model = await _deviceConfigurationRepository.InsertAsync(model);

            return ObjectMapper.Map<DeviceConfigurationModel, DeviceConfigurationResponse>(model);
        }
    }
}
